package com.vitaf.cash.app.common

import android.view.View
import com.vitaf.cash.app.VitafCashApplication
import com.vitaf.cash.app.base.VitafBaseFmtV2
import com.vitaf.cash.app.configs.VitafConfigs
import com.vitaf.cash.app.event.VitafEventData
import com.vitaf.cash.app.event.VitafEventPointType
import com.vitaf.cash.app.event.VitafEventSubscriber
import com.vitaf.cash.app.request.HttpHelper
import com.vitaf.cash.app.request.IHttpCallback
import com.vitaf.cash.app.ui.dialog.VitafCustomerDlog
import com.vitaf.cash.app.utils.VitafTitleBarUtil
import org.json.JSONObject

/**
 * CommonFragment
 * @Author wwdeng
 * @CreateTime: 2024/11/28 10:32
 * @Annotation：
 */
abstract class VitafComFmtV2: VitafBaseFmtV2(), VitafEventSubscriber, IVitafComViewV2 {
    //是否记录埋点
    private var isPageViewRecorded = false

    //客服
    var customerDialog: VitafCustomerDlog? = null


    override fun getAct(): VitafComActV2 {
        return context as VitafComActV2
    }

    /**
     * 返回
     */
    override fun onBackAction() {
        super.onBackAction()
        addEventPointRequestAction(VitafEventPointType.GOMBECREDIT_TRACK_CLICK.BACK_CLICK, pageName)
    }

    /**
     * 客服
     */
    fun onClickCustomer(){
        onClickCustomer(pageName)
    }

    /**
     * 客服
     */
    fun onClickCustomer(remark: String = pageName){
        addEventPointRequestAction(VitafEventPointType.GOMBECREDIT_TRACK_CLICK.CUSTOMER, remark)
        showCustomerDialog(remark)
    }

    override fun onStart() {
        super.onStart()
//        HttpHelper.setTag(this)
        VitafCashApplication.app.publisher.register(this)
    }

    /**
     * 设置Top padding
     */
    fun setViewPaddingTopStatusBarHeight(view: View?,topPadding: Int = VitafTitleBarUtil.getStatusBarHeight()){
        getAct().setViewPaddingTopStatusBarHeight(view,topPadding)
    }

    /**
     * 客服
     */
    fun showCustomerDialog(remark: String = pageName){
        if (customerDialog == null){
            customerDialog = VitafCustomerDlog(requireContext())
                .setGombeCreditCustomerEventRemark(remark)
                .setGombeCreditCustomerClickCallback(object : VitafCustomerDlog.IDialogCallback{
                    override fun onGombeCreditCallPhoneClick(phoneNumber: String) {
                    }
                    override fun onGombeCreditCopyEmailClick(email: String) {
                    }
                    override fun onGombeCreditFeedbackClick() {//Feedback
                    }
                })
            customerDialog?.setCancelable(true)
            customerDialog?.setCanceledOnTouchOutside(true)
        }
        customerDialog?.show()
    }

    /**
     * 错误提示
     */
    fun showErrorTipsDialog(tips: String,eventRemark: String = ""){
        getAct().showErrorTipsDialog(tips,eventRemark)
    }

    /**
     * post
     */
    fun doPostAction(httpUrl: String, params: JSONObject, callback: IHttpCallback){
        HttpHelper.post(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                callback.onRequestSuccess(data)
            }

            override fun onRequestError(error: JSONObject) {
                callback.onRequestError(error)
                showVitafToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                callback.onRequestFinish()
            }
        })
    }

    /**
     * 添加埋点
     */
    fun addEventPointRequestAction(eventName: String, eventRemark: String = "") {
        HttpHelper.addPoint(eventName,eventRemark)
    }

    /**
     * 发送事件通知
     */
    fun publishEventData(eventData: VitafEventData){
        VitafCashApplication.app.publisher.publish(eventData)
    }

    /**
     * 处理订阅事件
     */
    override fun onVitafPublishEvent(event: VitafEventData) {
    }

    override fun onResume() {
        super.onResume()
        if (!isPageViewRecorded) {
            isPageViewRecorded = true
            if (enablePageTrack){
                addEventPointRequestAction("${VitafConfigs.GOMBECREDIT_APPNAME}_${pageName}_start".uppercase())
            }
        }
    }

    override fun onPause() {
        super.onPause()
        customerDialog?.let {
            if (it.isShowing){
                it.dismiss()
            }
        }
        if (isRemoving){
            isPageViewRecorded = false
            if (enablePageTrack){
                addEventPointRequestAction("${VitafConfigs.GOMBECREDIT_APPNAME}_${pageName}_end".uppercase())
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        customerDialog = null
        HttpHelper.cancelTag(this)
        VitafCashApplication.app.publisher.detach(this)
    }
}